package com.raychi.demo.service;

import java.sql.Connection;
import java.util.List;

import com.raychi.demo.dao.CategoryDao;
import com.raychi.demo.entity.Category;
import com.raychi.demo.util.JdbcUtil;

public class CategoryService {
	
	private CategoryDao categoryDao;
	
	public List<Category> getCategorys(){
		Connection conn = null;
		List<Category> list = null;
		try{
			conn = JdbcUtil.getConnection();
			list = categoryDao.getCategorys(conn);
			conn.commit();
		}catch(Exception e){
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
		return list;
	}
	
	public void delete(Category category){
		Connection conn = null;
		try{
			conn = JdbcUtil.getConnection();
			categoryDao.delete(category, conn);
			conn.commit();
		}catch(Exception e){
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
	}
	
	public void update(Category category){
		Connection conn = null;
		try{
			conn = JdbcUtil.getConnection();
			categoryDao.update(category, conn);
			conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
	}
	
	public void save(Category category){
		Connection conn = null;
		try{
			conn = JdbcUtil.getConnection();
			categoryDao.save(category, conn);
			conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
	}
	
	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}
}
